package com.thon.service.project.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.DataDao;
import com.thon.dao.system.DicDataDao;
import com.thon.dao.util.Page;
import com.thon.entity.project.Data;
import com.thon.service.project.DataService;

@Service
@Transactional
public class DataServiceImpl implements DataService<Data> {

	@Autowired
	private DataDao dataDao;	

	@Autowired
	private DicDataDao dicDataDao;	

	@Override
	public Data read(Integer id) {		
		
	    return dataDao.get(id);
	}

	@Override
	public String create(Data data) {
		String msg = "success";		
		dataDao.save(data);
		return msg;
	}
		
	@Override
	public String create(Data[] data) {
		String msg = "success";	
		if(data!=null)
			for(Data da:data)
			{
				da.setCreatDate(new Date(System.currentTimeMillis()));
				da.setModifyDate(new Date(System.currentTimeMillis()));
				dataDao.save(da);
			}	
		return msg;
	}

	@Override
	public String delete(Integer id) {
		String msg = "success";		
		dataDao.delete(id);
		return msg;
	}

	@Override
	public String update(Data data) {		
		
		String msg = "success";
		if (data.getType() == null)
			return "dataType is null";
		data.setType(dicDataDao.get(data.getType().getId()));
		dataDao.update(data);
		return msg;
	}

	@Override
	public List<Data> readList() {

			return dataDao.readList();
	
	}

	@Override
	public Page<Data> readListPage(Integer pageNo, Integer pageSize) {
		Page<Data> page = new Page<Data>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page.desc("creatDate");
		
		return dataDao.readListPage(page);
	}


	


}